package com.bsoft.gol.editor.service.impl;

import com.bsoft.gol.editor.dao.EditorQuestionnaireOfficialDAO;
import com.bsoft.gol.editor.editor.EditorQuestionnaireOfficial;
import com.bsoft.gol.editor.service.EditorQuestionnaireOfficialApi;
import com.bsoft.gol.editor.utils.CommonUtils;
import ctd.spring.boot.annotation.SsdevService;
import hcn.util.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @program: Myproject
 * @description:
 * @author: zangsf
 * @create: 2022-12-08 15:34
 **/
@Service("editorQuestionnaireOfficialApi")
public class EditorQuestionnaireOfficialApiImpl implements EditorQuestionnaireOfficialApi {


    @Autowired
    private EditorQuestionnaireOfficialDAO editorQuestionnaireOfficialDAO;

    @Override
    public BaseResponse<List<EditorQuestionnaireOfficial>> queryEditorQuestionnaireOfficialList(EditorQuestionnaireOfficial qo) {
        BaseResponse<List<EditorQuestionnaireOfficial>> listBaseResponse = new BaseResponse<>();
        if (CommonUtils.isEmpty(qo.getQuestionnaireId())) {
            listBaseResponse.setCode(500);
            listBaseResponse.setMessage("问卷id不能为空");
            return listBaseResponse;
        }
        List<EditorQuestionnaireOfficial> editorQuestionnaireOfficials = editorQuestionnaireOfficialDAO.queryEditorQuestionnaireOfficialList(qo.getQuestionnaireId());
        listBaseResponse.setData(editorQuestionnaireOfficials);
        return listBaseResponse;
    }

    @Override
    public BaseResponse removeEditorQuestionnaireOfficial(EditorQuestionnaireOfficial qo) {
        BaseResponse listBaseResponse = new BaseResponse();
        if (CommonUtils.isEmpty(qo.getQuestionnaireId())) {
            listBaseResponse.setCode(500);
            listBaseResponse.setMessage("问卷id不能为空");
            return listBaseResponse;
        }
        editorQuestionnaireOfficialDAO.remove(qo.getOfficialId());
        return listBaseResponse;
    }

    @Override
    public BaseResponse addEditorQuestionnaireOfficial(EditorQuestionnaireOfficial qo) {
        BaseResponse listBaseResponse = new BaseResponse();
       if (CommonUtils.isNotEmpty(qo.getOfficialId())){
           EditorQuestionnaireOfficial editorQuestionnaireOfficial = editorQuestionnaireOfficialDAO.get(qo.getOfficialId());
           if (CommonUtils.isNotEmpty(editorQuestionnaireOfficial)){
               qo.setAlterTime(new Date());
               editorQuestionnaireOfficialDAO.update(qo);
           }else {
               qo.setOfficialId(UUID.randomUUID().toString().replace("-",""));
               qo.setAlterTime(new Date());
               qo.setCreateTime(new Date());
               editorQuestionnaireOfficialDAO.save(qo);
           }
       }else {
           qo.setOfficialId(UUID.randomUUID().toString().replace("-",""));
           qo.setAlterTime(new Date());
           qo.setCreateTime(new Date());
           editorQuestionnaireOfficialDAO.save(qo);
       }
        return listBaseResponse;
    }
}
